package com.heima.boot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/producer")
public class ProducerController {

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 发送消息的方法
     * @return
     */
    @GetMapping
    public String sendMessage(){
        //创建一个对象
        for (int i = 0; i <10 ; i++) {
            if(i%2==0){
                kafkaTemplate.send("topic_kafka_start", "wo shi kafka");
            }else{
                kafkaTemplate.send("topic_kafka_start", "wo shi itcast");
            }
        }

        return "success";
    }
}
